package com.hsyt.pms.controller.org;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hsyt.pfc.support.util.EnumToDataUtil;
import com.hsyt.pfc.support.util.LoggerUtil;
import com.hsyt.pms.api.common.ResultMessage;
import com.hsyt.pms.api.dto.org.OrgDto;
import com.hsyt.pms.api.entity.org.Org;
import com.hsyt.pms.api.enums.AbleStatus;
import com.hsyt.pms.api.enums.OrgType;
import com.hsyt.pms.api.enums.YesOrNo;
import com.hsyt.pms.api.vo.org.GridData;
import com.hsyt.pms.controller.base.BaseController;
import com.hsyt.pms.org.dubbo.OrgService;
import com.hsyt.pms.sys.dubbo.SysCityService;
import com.hsyt.pms.sys.dubbo.SysDictionaryService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: SysCityController
 * @Description: 城市服务控制层
 * @author wangze@hsyuntai.com
 * @date 2019年2月12日 下午2:45:51
 */
@RestController
@RequestMapping("/pms/org")
public class OrgController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrgController.class);

    @Reference(group = "pmsOrg", registry = "pms", version = "0.0.1")
    private OrgService orgService;

    @Reference(group = "pmsOrg", registry = "pms", version = "0.0.1")
    private SysCityService sysCityService;

    @Reference(group = "pmsOrg", registry = "pms", version = "0.0.1")
    private SysDictionaryService sysDictionaryService;

    @RequestMapping(value = "/page")
    public ModelAndView page(ModelMap model) {
        OrgDto orgDto = new OrgDto();
        orgDto.setStatus(AbleStatus.ENABLED.getCode());
        String orgTypeComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(OrgType.class, "value", "name");
        model.put("orgTypeComboData", orgTypeComboData);
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(AbleStatus.class);
        model.put("statusComboData", statusComboData);
        String isOrNotComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(YesOrNo.class);
        model.put("isOrNotComboData", isOrNotComboData);
        return new ModelAndView("org/orgMgr", model);
    }

    @RequestMapping(value = "/editPage")
    public ModelAndView editPage(ModelMap model, String orgType, String type, Long parentOrgId) {
        model.put("type", type);
        model.put("orgType", orgType);
        String orgTypeComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(OrgType.class, "value", "name");
        model.put("orgTypeComboData", orgTypeComboData);
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(AbleStatus.class);
        model.put("statusComboData", statusComboData);
        String isOrNotComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(YesOrNo.class);
        model.put("isOrNotComboData", isOrNotComboData);
        if (StringUtils.equals("viewPerson", type)) {
            return new ModelAndView("org/viewPerson", model);
        } else {
            return new ModelAndView("org/editOrg", model);
        }
    }

    @RequestMapping(value = "/doUndoOrg")
    public @ResponseBody
    ResultMessage doUndoOrg(@RequestBody OrgDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "操作成功");
        try {
            this.orgService.updateByPrimaryKey(dto);
        } catch (Exception e) {
            result.change(false, "操作失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysParam-doUndoSysCity error:{0} param:{1}", e.getMessage(), dto);
        }
        return result;
    }

    @RequestMapping(value = "/addOrg")
    public @ResponseBody
    ResultMessage addOrg(@RequestBody OrgDto orgDto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "新增成功");
        try {
            this.orgService.insertOrg(orgDto);
        } catch (Exception e) {
            result.change(false, "新增失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "org-addOrg error: {0}", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/editOrg")
    public @ResponseBody
    ResultMessage editOrg(@RequestBody OrgDto orgDto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "更新成功");
        try {
            this.orgService.updateByPrimaryKey(orgDto);
        } catch (Exception e) {
            result.change(false, "更新失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "org-editOrg error: {0}", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/queryOrgGrid")
    public @ResponseBody
    GridData queryOrgGrid(@RequestBody OrgDto dto) {
        return this.orgService.pageFindOrgrid(dto);
    }

    //-------------------以下通过ElasticSearch检索查询--------------

    @RequestMapping(value = "/createOrgEs")
    public @ResponseBody
    ResultMessage allEsOrg(@RequestBody OrgDto dto) {
        ResultMessage result = new ResultMessage(true, "创建Es索引成功");
        try {
            this.orgService.createOrgEs();
        } catch (Exception e) {
            result.change(false, "创建Es索引成功" + e.getMessage());
        }
        return result;
    }

}
